﻿#region Copyright 
/*-------------------------------------------------------------------------
* 命名空间名称/文件名:    SSPivot.Common.Helpers/DataMaskHelper 
* CLR版本:     4.0.30319.42000
* 机器名称:    DESKTOP-NHMLO8A
* 功 能：       N/A
* 类 名：       DataMaskHelper
* 创建时间：  2025/5/22 16:04:43
* 版本号：      v1.0
* 创建人:        xulong
*-------------------------------------------------------------------------*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace SSPivot.Common.Helpers
{
    /// <summary>
    /// 数据脱敏
    /// </summary>
    public class DataMaskHelper
    {
        public static readonly string PhoneMaskRegex = @"(\d{3})\d{4}(\d{4})";

        public static readonly string EmailMaskRegex = "(?<=.{2})[^@]+(?=.{2}@)";

        public static readonly string IPMaskRegex = @"(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})";

        /// <summary>
        /// 手机号脱敏
        /// </summary>
        /// <param name="input"></param>
        /// <param name="mask"></param>
        /// <returns></returns>
        public static string PhoneMask(string input, string mask = "****")
        {
            if (input.IsNull())
            {
                return input;
            }

            return Regex.Replace(input, PhoneMaskRegex, match => $"{match.Groups[1]?.Value}{mask}{match.Groups[2]?.Value}");
        }

        /// <summary>
        /// 邮箱脱敏
        /// </summary>
        /// <param name="input"></param>
        /// <param name="mask"></param>
        /// <returns></returns>
        public static string EmailMask(string input, string mask = "****")
        {
            if (input.IsNull())
            {
                return input;
            }

            return Regex.Replace(input, EmailMaskRegex, mask);
        }

        /// <summary>
        /// IP脱敏
        /// </summary>
        /// <param name="input"></param>
        /// <param name="mask"></param>
        /// <returns></returns>
        public static string IPMask(string input, string mask = "*")
        {
            if (input.IsNull())
            {
                return input;
            }

            return Regex.Replace(input, IPMaskRegex, match => $"{match.Groups[1]?.Value}.{mask}.{mask}.{match.Groups[4]?.Value}");
        }
    }
}
